#include "pch.h"
#include "db_interface.h"
#include "db_threadpool.h"
/*#include "entity_table.h"*/
/*#include "common/kbekey.h"*/
#include "db_interface_mysql.h"
/*#include "db_redis/db_interface_redis.h"*/
/*#include "server/serverconfig.h"*/
#include "threadpool.h"
#include "util.h"
//DBUtil* Singleton<DBUtil>::m_instance = NULL;

DBUtil g_DBUtil;

thread::ThreadPool* DBUtil::pThreadPool_ = new DBThreadPool();
DBInfo DBUtil::dbInfo_;
//-------------------------------------------------------------------------------------
DBUtil::DBUtil()
{
}

//-------------------------------------------------------------------------------------
DBUtil::~DBUtil()
{
}

//-------------------------------------------------------------------------------------
bool DBUtil::initThread()
{
	//if(strcmp(dbcfg.db_type, "mysql") == 0)
	{
		if (!mysql_thread_safe()) 
		{
			ASSERT(false);
		}
		else
		{
			mysql_thread_init();
		}
	}
	
	return true;
}

//-------------------------------------------------------------------------------------
bool DBUtil::finiThread()
{
	//if(strcmp(dbcfg.db_type, "mysql") == 0)
	{
		mysql_thread_end();
	}

	return true;
}

//-------------------------------------------------------------------------------------
bool DBUtil::initialize()
{
	return true;
}

//-------------------------------------------------------------------------------------
void DBUtil::finalise()
{
	pThreadPool()->finalise();
	SAFE_RELEASE(pThreadPool_);
}

//-------------------------------------------------------------------------------------
DBInterface* DBUtil::createInterface(bool showinfo)
{
	DBInterface* dbinterface = NULL;

	//if(strcmp(dbcfg.db_type, "mysql") == 0)
	{
		dbinterface = new DBInterfaceMysql();
	}
	//else if(strcmp(dbcfg.db_type, "redis") == 0)
	//{
	//	dbinterface = new DBInterfaceRedis();
	//}

	if(dbinterface == NULL)
	{
		LogError("DBUtil::createInterface: create db_interface error! type={mysql}\n");

		return NULL;
	}
	
	kbe_snprintf(dbinterface->db_type_, MAX_BUF, "%s", "mysql"/*dbcfg.db_type*/);
	dbinterface->db_port_ = convert<UINT32>(dbInfo_.m_port);
	kbe_snprintf(dbinterface->db_ip_, MAX_IP, "%s", dbInfo_.m_ip.c_str());
	kbe_snprintf(dbinterface->db_username_, MAX_BUF, "%s", dbInfo_.m_user.c_str());
	dbinterface->db_numConnections_ = 6;
	kbe_snprintf(dbinterface->db_password_, MAX_BUF, "%s", dbInfo_.m_pswd.c_str());

	if(!dbinterface->attach(DBUtil::dbname()))
	{
		LogError("DBUtil::createInterface: attach to database failed!\n\tdbinterface={%p}\n\targs={%s}\n",
			(void*)&dbinterface, dbinterface->c_str());

		delete dbinterface;
		return NULL;
	}
	else
	{
		if(showinfo)
		{
			LogInfo("DBUtil::createInterface[{%p}]: {%s}\n", (void*)&dbinterface, 
				dbinterface->c_str());
		}
	}

	return dbinterface;
}

//-------------------------------------------------------------------------------------
const char* DBUtil::dbname()
{
	return dbInfo_.m_database.c_str();
}

//-------------------------------------------------------------------------------------
const char* DBUtil::dbtype()
{
	return "mysql";// dbcfg.db_type;
}

//-------------------------------------------------------------------------------------
bool DBUtil::initInterface(const DBInfo& dbInfo)
{
	DBUtil::dbInfo_ = dbInfo;
	if(!pThreadPool_->isInitialize())
	{
		if(!pThreadPool_->createThreadPool(0, 
			6, 6))
			return false;
	}

// 	bool ret = true;
// 
// 	if(ret)
// 	{
// 		ret = dbi->checkEnvironment();
// 	}
// 	
// 	if(ret)
// 	{
// 		ret = dbi->checkErrors();
// 	}

	return true;
}

